<?

class CreateEmptyFile extends Button  {

    function render($sel) {
        return $this->renderDialogButton('createemptyfile','New file','Create new empty file');
    }

    function renderDialog($sel) {
        return '<form method="POST" action="'.Config::getLink($_GET['path'],'','&action=createemptyfile').'"><b>&nbsp;New name:</b> <input type="text" name="filename" id="filename"><input type="submit" value="Create" onClick=" if (document.getElementById(\'filename\').text.search(\'[^a-zA-Z0-9_+]\') != -1) { alert(\'Wrong name!\'); return false; }"></form>';
    }

    function action($sel) {
        $location = $_SERVER['REQUEST_URI'];
        $location = str_replace('action=createemptyfile','',$location);
        if (!isset($_POST['filename']) || ($_POST['filename'] == '')) {
            $location .= Config::getMessage(MESSAGE_ERROR,'Can\'t create file with empty name');
        } elseif (preg_match('[^a-zA-Z0-9_]',$_POST['filename'])) {
            $location .= Config::getMessage(MESSAGE_ERROR,'Can\'t create file: bad name');;
        } else {
            $file = $sel->getCurrentDirName().$_POST['filename'];
            if (!file_exists($file)) {
                @touch($file);
                $location .= Config::getMessage(MESSAGE_INFO,'File "<b>'.$_POST['filename'].'</b>" was created successfully');
            } else {
                $location .= Config::getMessage(MESSAGE_ERROR,'Can\'t create file - name "<b>'.$_POST['filename'].'</b>" already exists!');
            }
        }
        header('Location: '.$location);
        exit;
    }

}

?>